﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace HttpHandler
{
    /// <summary>
    /// 图片防盗链
    /// </summary>
    public class ImgHandler : IHttpHandler
    {
        const string DOMAIN = "www.eee114.com";
        const string ERRIMG = "/Content/daolian.jpg";
        public void ProcessRequest(HttpContext context)
        {
            // 获取文件服务器端物理路径
            string fileName = context.Server.MapPath(context.Request.FilePath);
            // 如果UrlReferrer为空，则显示一张默认的禁止盗链的图片
            if ((context.Request.UrlReferrer == null || context.Request.UrlReferrer.Host == null)
                && (context.Request.Url == null || context.Request.Url.Host == null))
            {
                context.Response.ContentType = "image/JPEG";
                context.Response.WriteFile(ERRIMG);
            }
            else
            {
                if ((context.Request.UrlReferrer != null
                    && context.Request.UrlReferrer.Host != null
                    && context.Request.UrlReferrer.Host.IndexOf(DOMAIN) >= 0)
                    || (context.Request.Url != null
                    && context.Request.Url.Host != null
                    && context.Request.Url.Host.IndexOf(DOMAIN) >= 0))
                {
                    context.Response.ContentType = "image/JPEG";
                    var image = new Bitmap(fileName);
                    image = AddWaterMark(image);
                    //将修改后的图片存入输出流
                    image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //释放水印图片
                    image.Dispose();
                    //输出发送到客户端，停止该页执行
                    context.Response.End();
                }
                else
                {
                    context.Response.ContentType = "image/JPEG";
                    context.Response.WriteFile(ERRIMG);
                }
            }
        }

        //给图片添加水印
        private Bitmap AddWaterMark(Bitmap image)
        {
            string text = System.Configuration.ConfigurationManager.AppSettings["WaterMarkText"] == null
                ? string.Empty
                : System.Configuration.ConfigurationManager.AppSettings["WaterMarkText"].ToString();
            int fontSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["WaterMarkTextSize"] == null
                ? "0"
                : System.Configuration.ConfigurationManager.AppSettings["WaterMarkTextSize"].ToString());
            if (!string.IsNullOrWhiteSpace(text))
            {
                Font font = new Font("Verdana", fontSize, FontStyle.Regular);
                FontStyle style = FontStyle.Bold | FontStyle.Italic;
                Font drawFont = new Font("Arial", 12, style);
                SolidBrush drawBrush = new SolidBrush(Color.Red);

                Graphics g = Graphics.FromImage(image);
                SizeF size = g.MeasureString(text, font);
                g.DrawString(text, font, drawBrush, image.Width - size.Width, image.Height - size.Height);
                g.Dispose();

            }
            return image;
        }
        public bool IsReusable
        {
            get { return true; }
        }
    }
}
